/**
 * 
 */
package expressions;

import java.util.ArrayList;
import java.util.HashMap;
import tester.Example;
import tester.TestMethod;
import tester.Tester;
import tester.cobertura.Instrument;

/**
 * Title : BEEvalVisitor
 * 
 * Javadoc Online at : http://www.ccs.neu.edu/home/ada2358/Doc/Satisfiability/index.html
 * @author Adam Blackwell
 * @version 1.0
 * @since April 2nd 2009
 */
@Example @Instrument
public class BEEvalVisitor implements BEVisitor<Boolean> {
  // I want to use this...
  public HashMap<String, Boolean> varsMap = new HashMap<String, Boolean>();
  // But I'm using this... Or not...
  // public ArrayList<Var> vars;
  
  /**
   * Default Constructor :
   */
  public BEEvalVisitor() {}
  
  /**
   * @param varsMap
   */
  public BEEvalVisitor(HashMap<String, Boolean> varsMap) {
    this.varsMap = varsMap;
  }
  
  /* (non-Javadoc)
   * @see expressions.BEVisitor#forAnd(expressions.BE, expressions.BE)
   */
  @Override public Boolean forAnd(BE left, BE right) {
    // TODO Auto-generated method stub
    return left.accept(this) && right.accept(this);
  }
  
  /* (non-Javadoc)
   * @see expressions.BEVisitor#forNot(expressions.BE)
   */
  @Override public Boolean forNot(BE be) {
    // TODO Auto-generated method stub
    return !be.accept(this);
  }
  
  /* (non-Javadoc)
   * @see expressions.BEVisitor#forOr(expressions.BE, expressions.BE)
   */
  @Override public Boolean forOr(BE left, BE right) {
    // TODO Auto-generated method stub
    return left.accept(this) || right.accept(this);
  }
  
  /* (non-Javadoc)
   * @see expressions.BEVisitor#forVar(expressions.BE)
   */
  @Override public Boolean forVar(BE be) {
    // TODO Auto-generated method stub
    if (varsMap.size() > 0) {
      return this.varsMap.get(((Var)be).getName());
    } else {
      return ((Var) be).isValue();
    }
  }
  
  
  /* *************** *
   * Testing :       *
   * *************** */
   
  @TestMethod
  public void tests(Tester t) {
    BEEvalVisitor evalVisit = new BEEvalVisitor();
    BE be1 = new And(new Var("x", true), new Var("y", false));
    BE be2 = new And(new Var("x", true), 
                      new Or(new Not(new Var("z", true)),
                             new Var("y", false)));
    BE be3 = new And(new Var("x", true), 
                      new Or(new Not(new Var("z", true)),
                             new Var("y", true)));
    t.checkExpect(be1.accept(evalVisit), false);
    t.checkExpect(be2.accept(evalVisit), false);
    t.checkExpect(be3.accept(evalVisit), true);
  }
}
